import React from 'react';
import ReactDOM from 'react-dom';
import Routers from './router';
import {createStore, applyMiddleware} from 'redux';
import {Provider} from 'react-redux';
import {REDUX_DATA} from '../util';
import axios from 'axios';
import thunkMiddleware from 'redux-thunk';
import {API_ADDRESS} from '../util/address';



const store = createStore(reducer,applyMiddleware(
    thunkMiddleware
  ));

//redux devtools
//window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

 function getMes()
 {
  return dispatch=>{
    return axios.get(`${API_ADDRESS}/getMes`)
      .then(
          response => response.data,
          error => console.log('An error occurred.', error)
      )
      .then(data =>
        dispatch({type:'update',data:{data}})
      )
  }
 }


store.dispatch(getMes()).then((a)=>console.log(store.getState()))


function reducer(state={data:[]},action)
{
  switch(action.type)
  {
    case 'update':
        return action.data;
    default:
        return state
  }
}


 ReactDOM.render(
            <div>
            <Provider store={store}>
                  <Routers/>
              </Provider>
            </div>
,document.getElementById('app'))
